import dayjs from 'dayjs'
import JSEncrypt from 'jsencrypt'

export function getRSAPublicKey(): string {
  return 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdLGep37lcc/CL1+NuapkFKT4MWFn0FC2BvA2ZSwVVOzM0hvgRJdVcF7xxbgeV0qXFm6MzS/suPeyT41xhXmFN+xKsg6/qd2Kt3aM9GcEXrMc3bwiOctm0q6BYu3WuavlzoGxYuwYs3ncysz8ZKsJMO2hSNkWOPr7j1HYALYjBLQIDAQAB'
}

export function getRSAResult(data: string): string {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(getRSAPublicKey())
  const timestamp = new Date().getTime()
  const before = `${timestamp}:${data}`
  const result = encryptor.encrypt(before)
  return result ? result : ''
}

export function formatDate(
  date: string | number | Date,
  format: string = 'YYYY-MM-DD HH:mm:ss'
): string {
  if (!date) return ''
  return dayjs(date).format(format)
}

export function isVideo(url: string): boolean {
  // 支持检测mp4等几种常见格式的视频, 忽略大小写
  return (
    url.toLowerCase().endsWith('.mp4') ||
    url.toLowerCase().endsWith('.avi') ||
    url.toLowerCase().endsWith('.mov') ||
    url.toLowerCase().endsWith('.flv') ||
    url.toLowerCase().endsWith('.wmv') ||
    url.toLowerCase().endsWith('.mpeg') ||
    url.toLowerCase().endsWith('.mpg')
  )
}

export function isImage(url: string): boolean {
  // 支持检测jpg, jpeg, png, gif, bmp, webp等几种常见格式的图片
  return (
    url.toLowerCase().endsWith('.jpg') ||
    url.toLowerCase().endsWith('.jpeg') ||
    url.toLowerCase().endsWith('.png') ||
    url.toLowerCase().endsWith('.gif') ||
    url.toLowerCase().endsWith('.bmp') ||
    url.toLowerCase().endsWith('.webp')
  )
}
